﻿--[[
   @brief    NUBannerBranch(单机游戏多分支选择)
   @author   jeffrywang
   @version  1.0
   @date     2016-12-28
--]]


base_utility.log("loading rail_branch_list.lua");
ierd_tgp.tpf_ui.Require("frame_bottom\\rail_branch.lua")

NURailBranchList = {

    kShadeTop = 0;  -- 顶部阴影高度
    kShadeWidth = 0;  -- 左右阴影宽度
    kExpectSpace = 0;  -- 期望与Banner底部的间隔
    kListSpaceForPlugin = 0;
    kListXOffsetForPlugin = 0;
    meunu_id = 0;
    current_game_id = 0;
    menue_id_to_branch_id = {};
    is_show = false;
    menu_item_hover_type = {
        hover_up = "9IMAGES:bottom_bar\\bottom_bar.png?379,130,387,138;bottom_bar\\bottom_bar.png?389,130,397,138;bottom_bar\\bottom_bar.png?399,130,407,138;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?389,140,397,148;bottom_bar\\bottom_bar.png?399,140,407,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?389,140,397,148;bottom_bar\\bottom_bar.png?399,140,407,148;Stretch;0;";
        hover_mid = "9IMAGES:bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?379,140,387,148;Stretch;0;";
        hover_down = "9IMAGES:bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?389,140,397,148;bottom_bar\\bottom_bar.png?399,140,407,148;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?389,140,397,148;bottom_bar\\bottom_bar.png?399,140,407,148;bottom_bar\\bottom_bar.png?379,150,387,158;bottom_bar\\bottom_bar.png?389,150,397,158;bottom_bar\\bottom_bar.png?399,150,407,158;Stretch;null;";
        hover_updown = "9IMAGES:bottom_bar\\bottom_bar.png?379,130,387,138;bottom_bar\\bottom_bar.png?389,130,397,138;bottom_bar\\bottom_bar.png?399,130,407,138;bottom_bar\\bottom_bar.png?379,140,387,148;bottom_bar\\bottom_bar.png?389,140,397,148;bottom_bar\\bottom_bar.png?399,140,407,148;bottom_bar\\bottom_bar.png?379,150,387,158;bottom_bar\\bottom_bar.png?389,150,397,158;bottom_bar\\bottom_bar.png?399,150,407,158;Stretch;0;"; 
    };
    
    init = function()
        ierd_tgp.tpf_ui.Log("[NURailBranchList]init");

        local main_uilib = ui_utility.uilib();
        local main_wnd_id = main_uilib:GetControlIDbyCustomID(CID_MAINFRAME_Dialog_MainFrame);
        if main_wnd_id ~= 0 then
            local const_ui_str = U8toCUIStr("WinMenu_BranchList");
            NURailBranchList.meunu_id = main_uilib:GetControlIDbyPath(const_ui_str, main_wnd_id);
            NURailBranchList.reset_menue_item();
        else
            ierd_tgp.tpf_ui.Log("[NURailBranchList][init]main_wnd_id is INVALID", true);
        end
    end;
        
    -- 输入参数为banner上菜单按钮相对屏幕的位置
    show = function(xoffset, yoffset, game_id)
        base_utility.log("[NURailBranchList][show]x:" .. tostring(xoffset) .. ", y:" .. tostring(yoffset)); 
                
        local pMenuControl = ui_utility.get_ctrl_ptr_by_id(NURailBranchList.meunu_id);
        if nil == pMenuControl then
            base_utility.log("[NURailBranchList][show]pMenuControl is NIL", true);
            return;
        end
        
        --根据游戏设置菜单项
        NURailBranchList.current_game_id = game_id;
        NURailBranchList.reset_menue_item();
        
        if NURailBranchList.refresh_list(game_id) == false then
            base_utility.log("[NURailBranchList][show] no info", true);
            return;
        end
        
        local rc = pMenuControl:GetRectEx(TRUE,TRUE);
        local h = rc.bottom - rc.top;
        yoffset = yoffset - h + NURailBranchList.kListSpaceForPlugin;
        xoffset = xoffset + NURailBranchList.kListXOffsetForPlugin;

        local new_rc = NURailBranchList.adjust_rc(xoffset, yoffset, rc);
        
        pMenuControl:MoveEx(new_rc,TRUE);
        pMenuControl:Show(TRUE);
    end;

    -- 显示
    fire_list_show = function(is_show)
        local params = {};
        params.is_show = is_show;
        params.game_id = NURailBranchList.current_game_id;
        event_center.send_event(frame_bottom_event.BRANCH_LIST_SHOW, base_utility.table_to_json(params));
    end;
    
    -- 获得新位置
    adjust_rc = function(xoffset, yoffset, rc)        
        --获取主窗口的位置
        local main_wnd = ui_utility.get_ctrl_ptr_by_cid(CID_MAINFRAME_Dialog_MainFrame);
        if nil == main_wnd then
            base_utility.log("[NURailBranchList][move]main_wnd is NIL", true);
            return rc;
        end
        
        local main_wnd_rc = main_wnd:GetRectEx(TRUE,TRUE);

        local w = rc.right - rc.left;
        local h = rc.bottom - rc.top;

        rc.left = xoffset - main_wnd_rc.left - NURailBranchList.kShadeWidth;
        rc.right = rc.left + w;
        rc.top = yoffset - main_wnd_rc.top - NURailBranchList.kShadeTop +
            NURailBranchList.kExpectSpace;
        rc.bottom = rc.top + h;
        
        return rc;
    end;

    hide = function()
        local pMenuControl = ui_utility.get_ctrl_ptr_by_id(NURailBranchList.meunu_id);
        if nil == pMenuControl then
            base_utility.log("[NURailBranchList][hide]pMenuControl is NIL", true);
            return;
        end
        
        if pMenuControl ~= nil then
            pMenuControl:Show(FALSE);
        end
    end;
    
    reset_menue_item = function()
        local pMenuControl = ui_utility.get_ctrl_ptr_by_id(NURailBranchList.meunu_id);
        if nil == pMenuControl then
            base_utility.log("[NURailBranchList][reset_menue_item]pMenuControl is NIL", true);
            return;
        end
        
        pMenuControl:RemoveAll();

        NURailBranchList.menue_id_to_branch_id = {};
    end;
    
    add_space_item = function()
    end;

    add_branch_item = function(branch_id, branch_name, branch_status, split)
        if  branch_id == nil or branch_id == 0 then
            base_utility.log("[NURailBranchList][add_branch_item]branch_id is NIL", true);
            return Tenio.INVALID_CONTROLID;
        end
        
        local item_id = NURailBranchList.add_branch_item_impl(branch_id);
        if item_id == Tenio.INVALID_CONTROLID then
            base_utility.log("[NURailBranchList][add_branch_item]item id is INVALID_CONTROLID", true);
            return item_id;
        end        
        
        ui_utility.show_only_child(item_id, "UobContent");
        local split_line_ptr = ui_utility.get_ctrl_ptr_by_path(item_id, "SplitLine");
        if nil ~= split_line_ptr and true == split then
            split_line_ptr:Show(TRUE);
        end
        NURailBranchList.set_item_data(item_id, game_id, branch_id, branch_name, branch_status);
        return item_id;
    end;
    
    add_branch_item_impl = function(branch_id)
        local pMenuControl = ui_utility.get_ctrl_ptr_by_id(NURailBranchList.meunu_id);
        if pMenuControl == nil then
            base_utility.log("[NURailBranchList][add_branch_item_impl]pMenuControl is NIL", true);
            return Tenio.INVALID_CONTROLID;
        end
        
        local const_ui_str = U8toCUIStr("RailBranch.tpf/TenioUILayout/UIObject/Children/UIObject?CntItem");
        local item_id = pMenuControl:AppendItem(const_ui_str);
        
        if item_id == Tenio.INVALID_CONTROLID then
            base_utility.log("[NURailBranchList][add_branch_item_impl]added item fail, "
                .. " for branch:" .. tostring(branch_id), true);
            return Tenio.INVALID_CONTROLID;
        end
        
        base_utility.log("[NURailBranchList][add_branch_item_impl]added item:" .. tostring(item_id)
            .. " for branch:" .. tostring(branch_id));
        NURailBranchList.menue_id_to_branch_id[item_id] = branch_id;
        
        return item_id;
    end;
    
    refresh_list = function(game_id)
        base_utility.log("[NURailBranchList][refresh_list]enter");
        local branches = frame_bottom_model.get_all_branch_infos(game_id);
        if branches == nil or branches.info == nil then
            base_utility.log("[NURailBranchList][refresh_list]branches is NIL", true);
            return false;
        end

        -- 插入分支名
        
        for k, info in ipairs(branches.info) do
            base_utility.log(string.format("[NURailBranchList][refresh_list]set server info (%d, %s, %d)",
                                      info.branch_id, info.app_name, info.status));
            local item_id = NURailBranchList.add_branch_item(info.branch_id, info.app_name, info.status, k ~= branches.count);
            if item_id ~= Tenio.INVALID_CONTROLID then
              if k == 1 then
                  ui_utility.setproperty_by_id(item_id, "BackgroundHover", NURailBranchList.menu_item_hover_type.hover_up); 
              elseif k == branches.count then
                  ui_utility.setproperty_by_id(item_id, "BackgroundHover", NURailBranchList.menu_item_hover_type.hover_down); 
              else
                  ui_utility.setproperty_by_id(item_id, "BackgroundHover", NURailBranchList.menu_item_hover_type.hover_mid); 
              end
            end
        end;
        return true;
    end;

    set_item_data = function(item_id, game_id, branch_id, branch_name, branch_status)
      local uob_content_id = ui_utility.get_ctrl_id_by_path(item_id, "UobContent");
      
      MainCommon.set_button_caption(ui_utility.uilib(), uob_content_id, "CntItemContent\\BtnBranchName",
          branch_name);
      
      local branchCtrl = ui_utility.get_ctrl_ptr_by_path(uob_content_id, "CntItemContent\\BtnBranchName");
      local branchRect = branchCtrl:GetRectEx(FALSE,TRUE);
      local uobCtrl = ui_utility.get_ctrl_ptr_by_id(uob_content_id);
      local uobRect = uobCtrl:GetRectEx(FALSE,TRUE);
      local uobWidth = uobRect.right - uobRect.left;
      branchRect.right = uobWidth;
      branchCtrl:MoveEx(branchRect,TRUE);
      
      --产品线的状态，1正常，-1删除
      MainCommon.set_enabled(ui_utility.uilib(), uob_content_id, "CntItemContent\\BtnBranchName", 
          branch_status > 0);      
      if branch_status > 0 then
          ui_utility.setproperty_by_id(item_id, "Disable", "false");
      else
          ui_utility.setproperty_by_id(item_id, "Disable", "true");
      end
    end;
};

------------------------------------UIEventListener-----------------------------------------------------------------

NURailBranchListUIEventListener = {
};

function NURailBranchListUIEventListener.on_item_click(uilib, id, item_id, item_index)
      base_utility.log("[NURailBranchList][on_item_click]item_id:" .. tostring(item_id));
      local game_id = NURailBranchList.current_game_id;
      local branch_id = NURailBranchList.menue_id_to_branch_id[item_id];
      base_utility.log("[NURailBranchList][on_item_click]branch selected:" .. tostring(branch_id) .. 
                                            ", for game:" .. tostring(game_id));
     
      if game_id == nil or game_id == 0 then
          base_utility.log("[NURailBranchList][on_item_click]invalid current game:" ..
              tostring(game_id), true);
          return;
      end

      NURailBranchList.hide();

      if branch_id ~= nil and branch_id ~= 0 then
          frame_bottom_control.switch_branch(game_id, branch_id);
      end
end


function NURailBranchListUIEventListener.on_show()
    base_utility.log("[NURailBranchList][on_show]");
    NURailBranchList.fire_list_show(true);
    is_show = true;
end

function NURailBranchListUIEventListener.on_hide()
    base_utility.log("[NURailBranchList][on_hide]");
    NURailBranchList.fire_list_show(false);
    is_show = false;
end

NURailBranchList.init();